local H = require "packages/new_heg/util"
local HC = require "packages/new_hegcomplements/util"
local U = require "packages/utility/utility"
local extension = Package:new("piracy")
extension.extensionName = "new_hegcomplements"
extension.game_modes_whitelist = { 'ex_heg_mode', 'mul_heg_mode' }

Fk:loadTranslationTable {
  ["piracy"] = "官盗",
  ["pt_hc_heg"] = "铂金大合集",
  ["tyt_hc_heg"] = "十年经典",
  ["wl_hc_heg"] = "群雄割据",
  ["new_hegcomplements"] = "国战补充",
}

local yuejin = General(extension, "tyt_hc_heg__n_yuejin", "wei", 4)

local xiaoguo = fk.CreateTriggerSkill{
  name = "tyt_hc__xiaoguo",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and target.phase == Player.Start and not player:isKongcheng() and not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForDiscard(player, 1, 998, true, self.name, true, ".|.|.|.|.|basic", "#tyt_hc__xiaoguo-invoke::"..target.id, true)
    if #card > 0 then
      self.cost_data = {cards = card, tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data.cards, self.name, player, player)
    local x, y = #self.cost_data.cards, 0
    if #target:getCardIds(Player.Equip) > 0 then
      local equips = room:askForCardsChosen(player, target, math.min(x, #target:getCardIds(Player.Equip)), math.min(x, #target:getCardIds(Player.Equip)), "e", self.name)
      if #equips > 0 then
        local count = #(room.logic.event_recorder[GameEvent.MoveCards] or Util.DummyTable)
        room:throwCard(equips, self.name, target, player)
        local e = HC.GetRecentMoveEventByskill(room, self.name, count)
        if e then
          for _, move in ipairs(e.data) do
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).type == Card.TypeEquip then
                y = y + 1
              elseif Fk:getCardById(info.cardId).type == Card.TypeBasic then
                x = x + 1
              end
            end
          end
        end
      end
    end
    if x > y then
      if not player.dead then
        player:drawCards(1, self.name)
      end
      if not target.dead then
        room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = self.name,
      }
      end
    end
  end,
}
yuejin:addSkill(xiaoguo)
yuejin:addCompanions("ld__n_lidian")

Fk:loadTranslationTable{
  ["tyt_hc_heg__n_yuejin"] = "乐进",
  ["#tyt_hc_heg__n_yuejin"] = "奋强突固",
  ["illustrator:tyt_hc_heg__n_yuejin"] = "巴萨小马",

  ["tyt_hc__xiaoguo"] = "骁果",
  [":tyt_hc__xiaoguo"] = "其他角色的准备阶段，你可以弃置至少一张基本牌，然后弃置其装备区等量张牌，若你弃置的装备牌数小于基本牌数，你摸一张牌并对其造成1点伤害。",
  ["#tyt_hc__xiaoguo-invoke"] = "你可弃置至少一张基本牌，对 %dest 发动〖骁果〗",

  ["$tyt_hc__xiaoguo1"] = "三军听我号令，不得撤退！",
  ["$tyt_hc__xiaoguo2"] = "看我先登城头，立下首功！",
  ["~tyt_hc_heg__n_yuejin"] = "箭疮发作，吾命休矣。",
}

local guohuai = General(extension, "wl_hc_heg__guohuai", "wei", 4)
guohuai.mainMaxHpAdjustedValue = -1

local duanshi = fk.CreateTriggerSkill {
  name = "wl_hc__duanshi",
  frequency = Skill.Compulsory,
  anim_type = "control",
  relate_to_place = "m",
  events = { fk.BeforeDrawCard },
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self) and data.skillName == "kill" and data.num > 0) then return end
    local death = player.room.logic:getCurrentEvent()
    return death and death.event == GameEvent.Death and H.compareKingdomWith(player, player.room:getPlayerById(death.data[1].who))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    data.num = data.num - 1
    room:drawCards(player, 1, self.name)
  end,
}

local jingce = fk.CreateTriggerSkill {
  name = "wl_hc__jingce",
  events = { fk.EventPhaseStart },
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player.phase == Player.Finish and player:hasSkill(self)) then return end
    local phase = {}
    if #player.room.logic:getEventsOfScope(GameEvent.UseCard, 998, function(e)
          local use = e.data[1]
          return use.from == player.id
        end, Player.HistoryTurn) >= player.hp
    then
      table.insert(phase, Player.Play)
    end
    local num = 0
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 998, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          num = num + #move.moveInfo
        end
      end
    end, Player.HistoryTurn)
    if num >= player.hp then
      table.insert(phase, Player.Draw)
    end
    if #phase > 0 then
      self.cost_data = phase
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    for _, phase in ipairs(self.cost_data) do
      player:gainAnExtraPhase(phase)
    end
  end,
}


guohuai:addSkill(duanshi)
guohuai:addSkill(jingce)
guohuai:addCompanions({ "hs__n_xiahouyuan", "hs__n_zhanghe" })

Fk:loadTranslationTable {
  ["wl_hc_heg__guohuai"] = "郭淮",
  ["#wl_hc_heg__guohuai"] = "垂问秦雍",
  ["illustrator:wl_hc_heg__guohuai"] = "心中一凛",

  ["wl_hc__duanshi"] = "断势",
  [":wl_hc__duanshi"] = "主将技，锁定技，此武将牌减少半个阴阳鱼；一名角色因杀死与你势力相同的其他角色执行奖惩而摸牌时，其少摸一张牌，然后你摸一张牌。",
  ["wl_hc__jingce"] = "精策",
  [":wl_hc__jingce"] = "结束阶段，若本回合置入弃牌堆的牌数不小于X，你可以获得一个额外的摸牌阶段；若本回合你使用过的牌数不小于X，你可以获得一个额外的出牌阶段。（X为你的体力值）",

  ["$wl_hc__duanshi1"] = "吾已料敌布防，蜀军休想进犯！",
  ["$wl_hc__duanshi2"] = "诸君依策行事，定保魏境无虞。",
  ["$wl_hc__jingce1"] = "精细入微，策敌制胜。",
  ["$wl_hc__jingce2"] = "妙策如神，精兵强将，安有不胜之理？",
  ["~wl_hc_heg__guohuai"] = "岂料姜维……空手接箭！",
}

local zongyu = General(extension, "tyt_hc_heg__zongyu", "shu", 3)

local chengshang = fk.CreateTriggerSkill{
  name = "tyt_hc__chengshang",
  anim_type = "drawcard",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and data.tos and
      table.find(TargetGroup:getRealTargets(data.tos), function(id) return not H.compareKingdomWith(player.room:getPlayerById(id), player) end)
      and not data.damageDealt and player.room:getCurrent() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, data.card.suit == Card.NoSuit and "#tyt_hc__chengshang-invoke-nosuit" or
      ("#tyt_hc__chengshang-invoke:::" .. data.card:getSuitCompletedString(true)))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:drawCards(player, 1, self.name)
    if data.card.suit == Card.NoSuit or player.dead then return end
    local mark = {}
    table.insert(mark, data.card:getSuitCompletedString(true))
    table.insert(mark, data.card.name)
    room:setPlayerMark(player, "@tyt_hc__chengshang-phase", mark)
    local all_names = {}
    for _, card in ipairs(Fk.cards) do
      if not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and not card.is_derived and card:compareSuitWith(data.card) and card:compareNumberWith(data.card) then
        if (card.type == Card.TypeBasic or card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick) then
          table.insertIfNeed(all_names, card.name)
        end
      end
    end
    table.removeOne(all_names, data.card.name)
    if #all_names > 0 then
      room:setPlayerMark(player, "tyt_hc__chengshang-phase", all_names)
      room:handleAddLoseSkills(player, "tyt_hc__chengshang-viewas&", nil, false, false)
      room.logic:getCurrentEvent():findParent(GameEvent.Phase):addCleaner(function()
        room:handleAddLoseSkills(player, "-tyt_hc__chengshang-viewas&", nil, false, false)
      end)
    end
  end,
}

local chengshang_viewas = fk.CreateViewAsSkill{
  name = "tyt_hc__chengshang-viewas&",
  handly_pile = true,
  mute = true,
  interaction = function(self, player)
    local all_names = player:getMark("tyt_hc__chengshang-phase")
    return UI.ComboBox {
      choices = U.getViewAsCardNames(player, "tyt_hc__chengshang-viewas&", all_names),
      all_choices = all_names,
    }
  end,
  card_filter = function(self, to_select, selected, player)
    return #selected == 0 and table.contains(player:getHandlyIds(), to_select)
  end,
  view_as = function(self, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return end
    if #cards ~= 1 then return end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = function(self, player)
    return #U.getViewAsCardNames(player, "tyt_hc__chengshang-viewas&", player:getMark("tyt_hc__chengshang-phase")) > 0
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:notifySkillInvoked(player, "tyt_hc__chengshang", "special")
    player:broadcastSkillInvoke(chengshang.name)
  end,
  dynamic_desc = function(self, player)
    local all_names = player:getMark("tyt_hc__chengshang-phase")
    if #all_names == 0 then return "tyt_hc__chengshang-viewas&" end
    local text = "tyt_hc__chengshang-viewas&-text:"
    local n, m = #all_names, 0
    for i, name in ipairs(all_names) do
      if string.find(name, "__slash") or name == "heg__nullification" then
        text = text .. "tyt_hc__chengshang-" .. name
        m = m + 1
      else
        text = text .. "tyt_hc__chengshang-left:" .. name .. ":tyt_hc__chengshang-right"
        m = m + 3
      end
      if i < n - 1 then
        text = text .. ":tyt_hc__chengshang-spilt:"
        m = m + 1
      elseif i == n-1 then
        text = text .. ":tyt_hc__chengshang-spiltlast:"
        m = m + 1
      end
    end
    for i = m, 16, 1 do
      text = text .. ":"
    end
    return text
  end,
}

Fk:addSkill(chengshang_viewas)

zongyu:addSkill("ce_hc__qiao")
zongyu:addSkill(chengshang)

Fk:loadTranslationTable{
  ["tyt_hc_heg__zongyu"] = "宗预",
  ["#tyt_hc_heg__zongyu"] = "九酝鸿胪",
  ["illustrator:tyt_hc_heg__zongyu"] = "铁杵文化",

  ["tyt_hc__chengshang"] = "承赏",
  [":tyt_hc__chengshang"] = "每阶段限一次，当你于出牌阶段内使用指定有与你势力不同或未确定势力角色为目标的牌结算后，若此牌未造成伤害，你可以摸一张牌，"..
  "若如此做，此阶段你可以将一张手牌当与你此次使用的牌花色点数均相同的另一张基本牌或普通锦囊牌使用。",

  ["tyt_hc__chengshang-viewas&"] = "承赏[转]",
  [":tyt_hc__chengshang-viewas&"] = "出牌阶段，你可以将一张手牌当其他牌使用（可转化的牌详见玩家技能页）。",
  [":tyt_hc__chengshang-viewas&-text"] = "出牌阶段，你可以将一张手牌当{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}使用。",
  ["tyt_hc__chengshang-left"] = "【",
  ["tyt_hc__chengshang-right"] = "】",
  ["tyt_hc__chengshang-spilt"] = "、",
  ["tyt_hc__chengshang-spiltlast"] = "或",
  ["tyt_hc__chengshang-fire__slash"] = "火【杀】",
  ["tyt_hc__chengshang-thunder__slash"] = "雷【杀】",
  ["tyt_hc__chengshang-heg__nullification"] = "国【无懈可击】",

  ["#tyt_hc__chengshang-invoke"] = "是否发动〖承赏〗，摸一张牌，本阶段可以把手牌转化为 %arg 的其他基本牌或普通锦囊牌",
  ["#tyt_hc__chengshang-invoke-nosuit"] = "是否发动〖承赏〗，摸一张牌",
  ["@tyt_hc__chengshang-phase"] = "承赏",

  ["$tyt_hc__chengshang1"] = "嘉其抗直，甚爱待之。",
  ["$tyt_hc__chengshang2"] = "为国鞠躬，必受封赏。",
  ["~tyt_hc_heg__zongyu"] = "吾年逾七十，唯少一死耳……",
}

local taishici = General(extension, "tyt_hc_heg__n_taishici", "wu", 4)
local tianyi = fk.CreateActiveSkill{
  name = "tyt_hc__tianyi",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return #selected == 0 and to_select ~= player.id and player:canPindian(Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({target}, self.name)
    if pindian.results[target.id].winner == player then
      room:addPlayerMark(player, "tyt_hc__tianyi_win-turn", 1)
    else
      room:addPlayerMark(player, "tyt_hc__tianyi_lose-turn", 1)
    end
  end,
}
local tianyi_targetmod = fk.CreateTargetModSkill{
  name = "#tyt_hc__tianyi_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("tyt_hc__tianyi_win-turn") > 0 and scope == Player.HistoryPhase then
      return 1
    end
  end,
  bypass_distances =  function(self, player, skill)
    return skill.trueName == "slash_skill" and player:getMark("tyt_hc__tianyi_win-turn") > 0
  end,
  extra_target_func = function(self, player, skill)
    if skill.trueName == "slash_skill" and player:getMark("tyt_hc__tianyi_win-turn") > 0 then
      return 1
    end
  end,
}
local tianyi_prohibit = fk.CreateProhibitSkill{
  name = "#tyt_hc__tianyi_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("tyt_hc__tianyi_lose-turn") > 0 and card.trueName == "slash"
  end,
}

local hanzhan = fk.CreateTriggerSkill{
  name = "tyt_hc__hanzhan",
  mute = true,
  events = {fk.PindianResultConfirmed},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    local other = data.from == player and data.to or data.from
    local winner = data.winner
    if winner then
      if winner == player then
        return player:hasShownSkill(self) and not other.dead and #player:getCardIds("e") > 0
      else
        return #other:getCardIds("e") > 0
      end
    else
      return player:hasShownSkill(self) and not other.dead and #player:getCardIds("e") > 0 or #other:getCardIds("e") > 0
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local other = data.from == player and data.to or data.from
    local winner = data.winner
    if winner then
      self.cost_data = winner == player and {other} or {player}
      self:doCost(event, target, player, data)
    else
      local targets = {player.id, other.id}
      player.room:sortPlayersByAction(targets)
      targets = table.map(targets, Util.Id2PlayerMapper)
      for _, t in ipairs(targets) do
        if t == player then
          if player:hasSkill(self) and not player.dead and #other:getCardIds("e") > 0 then
            self.cost_data = {player}
            self:doCost(event, target, player, data)
          end
        else
          if player:hasShownSkill(self) and not other.dead and #player:getCardIds("e") > 0 then
            self.cost_data = {other}
            self:doCost(event, target, player, data)
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local getplayer = self.cost_data[1]
    local to = data.from == getplayer and data.to or data.from
    return player.room:askForSkillInvoke(getplayer, self.name, data, "#tyt_hc__hanzhan-invoke::"..to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local getplayer = self.cost_data[1]
    local to = data.from == getplayer and data.to or data.from
    player:broadcastSkillInvoke(self.name)
    room:doIndicate(getplayer.id, {to.id})
    if player == getplayer then
      room:notifySkillInvoked(player, self.name, "control")
    else
      room:notifySkillInvoked(player, self.name, H.compareKingdomWith(getplayer, to) and "support" or "negative")
    end
    local card = room:askForCardChosen(getplayer, to, "e", self.name)
    room:obtainCard(getplayer, card, false, fk.ReasonPrey, getplayer.id, self.name)
  end,
}

tianyi:addRelatedSkill(tianyi_targetmod)
tianyi:addRelatedSkill(tianyi_prohibit)
taishici:addSkill(tianyi)
taishici:addSkill(hanzhan)

Fk:loadTranslationTable{
  ['tyt_hc_heg__n_taishici'] = '太史慈',
  ["#tyt_hc_heg__n_taishici"] = "笃烈之士",
  ["illustrator:tyt_hc_heg__n_taishici"] = "biou09",

  ["tyt_hc__tianyi"] = "天义",
  [":tyt_hc__tianyi"] = "出牌阶段限一次，你可以与一名角色拼点：若你赢，在本回合结束之前，你可以多使用一张【杀】、使用【杀】无距离限制且可以多选择一个目标；"..
  "若你没赢，本回合你不能使用【杀】。",
  ["tyt_hc__hanzhan"] = "酣战",
  [":tyt_hc__hanzhan"] = "当你拼点后，没赢的角色可以获得此次拼点的另一名角色装备区里的一张牌。",
  ["#tyt_hc__hanzhan-invoke"] = "酣战：是否获得 %dest 装备区的一张牌",

  ["$tyt_hc__tianyi1"] = "天降大任，速战解围！",
  ["$tyt_hc__tianyi2"] = "义不从之，天必不佑！",
  ["$tyt_hc__hanzhan1"] = "伯符，且与我一战！",
  ["$tyt_hc__hanzhan2"] = "与君酣战，快哉快哉！",
  ["~tyt_hc_heg__n_taishici"] = "无妄之灾，难以避免……",
}

local dingfeng = General(extension, "tyt_hc_heg__n_dingfeng", "wu", 4)
local duanbing = fk.CreateTriggerSkill{
  name = "tyt_hc__duanbing",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared, fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card.trueName == "slash" then
      if event == fk.AfterCardTargetDeclared then
        return table.find(player.room:getUseExtraTargets(data), function(id)
          return player:distanceTo(player.room:getPlayerById(id)) == 1
        end)
      else
        return U.isOnlyTarget(data.to, data, event)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = table.filter(room:getUseExtraTargets(data), function(id)
        return player:distanceTo(room:getPlayerById(id)) == 1
      end)
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#tyt_hc__duanbing-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = {tos = tos}
        return true
      end
    else
      self.cost_data = {tos = {data.to}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insert(data.tos, {self.cost_data.tos[1]})
    else
      data.fixedResponseTimes = data.fixedResponseTimes or {}
      data.fixedResponseTimes["jink"] = 2
    end
  end,
}
local fenxun = fk.CreateActiveSkill{
  name = "tyt_hc__fenxun",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  prompt = "#tyt_hc__fenxun",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:addTableMark(player, "tyt_hc__fenxun-turn", effect.tos[1])
    room:throwCard(effect.cards, self.name, player, player)
  end,
}
local fenxun_distance = fk.CreateDistanceSkill{
  name = "#tyt_hc__fenxun_distance",
  fixed_func = function(self, from, to)
    if table.contains(from:getTableMark("tyt_hc__fenxun-turn"), to.id) then
      return 1
    end
  end,
}
fenxun:addRelatedSkill(fenxun_distance)
dingfeng:addSkill(duanbing)
dingfeng:addSkill(fenxun)
Fk:loadTranslationTable{
  ["tyt_hc_heg__n_dingfeng"] = "丁奉",
  ["#tyt_hc_heg__n_dingfeng"] = "清侧重臣",
  ["illustrator:tyt_hc_heg__n_dingfeng"] = "魔鬼鱼",

  ["tyt_hc__duanbing"] = "短兵",
  [":tyt_hc__duanbing"] = "你使用【杀】时可以额外选择一名距离为1的其他角色为目标；当你使用【杀】指定唯一目标后，该角色需依次使用两张【闪】才能抵消此【杀】。",
  ["tyt_hc__fenxun"] = "奋迅",
  [":tyt_hc__fenxun"] = "出牌阶段限一次，你可以弃置一张牌并选择一名其他角色，令你与其的距离视为1，直到回合结束。",
  ["#tyt_hc__duanbing-choose"] = "短兵：你可以额外选择一名距离为1的其他角色为目标",
  ["#tyt_hc__fenxun"] = "奋迅：弃一张牌，你与一名角色的距离视为1直到回合结束",

  ["$tyt_hc__duanbing1"] = "众将官，短刀出鞘。",
  ["$tyt_hc__duanbing2"] = "短兵轻甲也可取汝性命！",
  ["$tyt_hc__fenxun1"] = "取封侯爵赏，正在今日！",
  ["$tyt_hc__fenxun2"] = "给我拉过来！",
  ["~tyt_hc_heg__n_dingfeng"] = "这风，太冷了……",
}

local lukang = General(extension, "tyt_hc_heg__n_lukang", "wu", 4)
lukang.mainMaxHpAdjustedValue = -1

local jueyan = fk.CreateTriggerSkill {
  name = "tyt_hc__jueyan",
  mute = true,
  relate_to_place = "m",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Start and not player:isAllNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "$Judge", "$Equip", "$Hand", "Cancel" }
    local choices = {}
    if #player:getCardIds("j") > 0 then
      table.insert(choices, "$Judge")
    end
    if #player:getCardIds("e") > 0 then
      table.insert(choices, "$Equip")
    end
    if #player:getCardIds("h") > 0 then
      table.insert(choices, "$Hand")
    end
    table.insert(choices, "Cancel")
    if #choices > 0 then
      local choice = room:askForChoice(player, choices, self.name, "#tyt_hc__jueyan-invoke", false, all_choices)
      if choice ~= "Cancel" then
        self.cost_data = choice
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@tyt_hc__jueyan-turn", self.cost_data)
    player:broadcastSkillInvoke(self.name)
    if self.cost_data == "$Judge" then
      room:notifySkillInvoked(player, self.name, "special")
      room:handleAddLoseSkills(player, "tyt_hc__jizhi", nil)
      room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
        room:handleAddLoseSkills(player, "-tyt_hc__jizhi", nil, true, false)
      end)
      player:skip(Player.Judge)
    elseif self.cost_data == "$Equip" then
      room:notifySkillInvoked(player, self.name, "drawcard")
      room:drawCards(player, 3, self.name)
      room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, 3)
    elseif self.cost_data == "$Hand" then
      room:notifySkillInvoked(player, self.name, "offensive")
      room:addPlayerMark(target, MarkEnum.SlashResidue .. "-turn", 3)
    end
  end,
}

local jueyan_delay = fk.CreateTriggerSkill {
  name = "#tyt_hc__jueyan_delay",
  mute = true,
  frequency = Skill.Compulsory,
  events = { fk.TurnEnd },
  can_trigger = function(self, event, target, player, data)
    if not (player == target and player:getMark("@tyt_hc__jueyan-turn")) then return end
    if player:getMark("@tyt_hc__jueyan-turn") == "$Judge" then
      return table.find(player:getCardIds("j"), function (id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    elseif player:getMark("@tyt_hc__jueyan-turn") == "$Equip" then
      return table.find(player:getCardIds("e"), function (id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    elseif player:getMark("@tyt_hc__jueyan-turn") == "$Hand" then
      return table.find(player:getCardIds("h"), function (id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("@tyt_hc__jueyan-turn") == "$Judge" then
      room:notifySkillInvoked(player, jueyan.name, "defensive")
      player:throwAllCards("j")
    elseif player:getMark("@tyt_hc__jueyan-turn") == "$Equip" then
      room:notifySkillInvoked(player, jueyan.name, "negative")
      player:throwAllCards("e")
    elseif player:getMark("@tyt_hc__jueyan-turn") == "$Hand" then
      room:notifySkillInvoked(player, jueyan.name, "negative")
      player:throwAllCards("h")
    end
  end,
}

local keshou_filter = fk.CreateActiveSkill {
  name = "#tyt_hc__keshou_filter",
  min_card_num = 2,
  max_card_num = 2,
  card_filter = function(self, to_select, selected)
    return table.every(selected, function(id)
      return Fk:getCardById(to_select).color == Fk:getCardById(id).color
    end)
  end,
  target_filter = Util.FalseFunc,
  can_use = Util.FalseFunc,
}

local keshou = fk.CreateTriggerSkill {
  name = "tyt_hc__keshou",
  anim_type = "defensive",
  events = { fk.DamageInflicted, fk.AfterCardsMove },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.DamageInflicted then return player == target and #target:getCardIds("he") > 1 end
    if not H.getKingdomPlayersNum(player.room)[H.getKingdom(player)] == 1 then return end
    local n = 0
    for _, move in ipairs(data) do
      if move.from == player.id and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            n = n + 1
            if n > 1 then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      local result, dat = room:askForUseActiveSkill(player, "#tyt_hc__keshou_filter", "#ld__n_keshou:::" .. data.damage,
        true)
      if result then
        self.cost_data = dat.cards
        return true
      end
    else
      return room:askForSkillInvoke(player, self.name)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      room:throwCard(self.cost_data, self.name, player, player)
      data.damage = data.damage - 1
    else
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.|heart,diamond|.|.|.",
      }
      room:judge(judge)
      if judge.card.color == Card.Red and not player.dead then
        player:drawCards(1, self.name)
      end
    end
  end,
}

local jizhi = fk.CreateTriggerSkill {
  name = "tyt_hc__jizhi",
  anim_type = "drawcard",
  events = { fk.CardUsing },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card:isCommonTrick() and
        (not data.card:isVirtual() or #data.card.subcards == 0)
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
  end,
}

jueyan:addRelatedSkill(jueyan_delay)
lukang:addSkill(jueyan)
keshou:addRelatedSkill(keshou_filter)
lukang:addSkill(keshou)
lukang:addRelatedSkill(jizhi)
lukang:addCompanions("hs__n_luxun")

Fk:loadTranslationTable {
  ["tyt_hc_heg__n_lukang"] = "陆抗",
  ["#tyt_hc_heg__n_lukang"] = "社稷之瑰宝",
  ["illustrator:tyt_hc_heg__n_lukang"] = "zoo",

  ["tyt_hc__jueyan"] = "决堰",
  [":tyt_hc__jueyan"] = "主将技，此武将牌减少半个阴阳鱼；准备阶段，你可以选择一个你有牌的区域，若为：判定区，本回合你拥有“集智”并跳过判定阶段；装备区，" ..
      "你摸三张牌且本回合手牌上限+3；手牌区，本回合你使用【杀】的次数+3。若如此做，此回合结束时你弃置此区域的所有牌。",
  ["tyt_hc__keshou"] = "恪守",
  [":tyt_hc__keshou"] = "当你受到伤害时，你可以弃置两张颜色相同的牌，令此伤害-1；当你一次性弃置至少两张牌后，若没有与你势力相同的其他角色，你可以判定，若结果为红色，你摸一张牌。",
  ["tyt_hc__jizhi"] = "集智",
  [":tyt_hc__jizhi"] = "当你使用非转化的普通锦囊牌时，你可摸一张牌。",
  ["#tyt_hc__jueyan_delay"] = "决堰",

  ["@tyt_hc__jueyan-turn"] = "决堰",
  ["#tyt_hc__jueyan-invoke"] = "你可以发动〖决堰〗，选择一个区域，回合结束时弃置此区域的所有牌",
  ["#tyt_hc__keshou_filter"] = "恪守",
  ["#tyt_hc__keshou"] = "恪守：是否弃置两张颜色相同的牌，令你受到的%arg点伤害-1",

  ["$tyt_hc__jueyan1"] = "毁堰坝之计，实为阻晋粮道。",
  ["$tyt_hc__jueyan2"] = "堰坝毁之，可令敌军自退。",
  ["$tyt_hc__keshou1"] = "城固兵足，无所忧患。",
  ["$tyt_hc__keshou2"] = "攻城易之，守城难矣。",
  ["$tyt_hc__jizhi"] = "智父安能有愚子乎？",
  ["~tyt_hc_heg__n_lukang"] = "吾即亡矣，吴又能存几时？",
}

local dongzhuo = General(extension, "tyt_hc_heg__n_dongzhuo", "qun", 4)

local hengzheng = fk.CreateTriggerSkill{
  name = 'tyt_hc__hengzheng',
  anim_type = "big", -- 神杀特色
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Draw and
      (player.hp == 1 or player:isKongcheng()) and
      table.find(player.room:getOtherPlayers(player, false), function(p) return not p:isAllNude() end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player, true)) do
      if not p:isAllNude() then
        local id = room:askForCardChosen(player, p, "hej", self.name)
        room:obtainCard(player, id, false)
      end
      if player.dead then
        break
      end
    end
    return true
  end,
}

local baoling = fk.CreateTriggerSkill{
  name = "tyt_hc__baoling",
  relate_to_place = 'm',
  anim_type = "big",
  events = {fk.EventPhaseEnd},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and H.hasGeneral(player, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    H.removeGeneral(room, player, true)
    if player.dead then return end
    room:changeMaxHp(player, 3)
    if player.dead then return end
    room:recover {
      who = player,
      num = 3,
      skillName = self.name
    }
    if player.dead then return end
    room:handleAddLoseSkills(player, "tyt_hc__benghuai")
  end,
}

local benghuai = fk.CreateTriggerSkill{
  name = "tyt_hc__benghuai",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and
      table.find(player.room.alive_players, function(p) return p.hp < player.hp end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"loseHp", "loseMaxHp", "#tyt_hc__benghuai-both"}, self.name)
    if choice == "loseMaxHp" then
      room:changeMaxHp(player, -1)
    elseif choice == "loseHp" then
      room:loseHp(player, 1, self.name)
    else
      room:loseHp(player, 1, self.name)
      if player.dead then return end
      room:changeMaxHp(player, -1)
      if player.dead then return end
      player:gainAnExtraPhase(Player.Draw)
    end
  end,
}

dongzhuo:addSkill(hengzheng)
dongzhuo:addSkill(baoling)
dongzhuo:addRelatedSkill(benghuai)

Fk:loadTranslationTable{
  ["tyt_hc_heg__n_dongzhuo"] = "董卓",
  ["#tyt_hc_heg__n_dongzhuo"] = "魔王",
  ["illustrator:tyt_hc_heg__n_dongzhuo"] = "巴萨小马",

  ['tyt_hc__hengzheng'] = '横征',
  [':tyt_hc__hengzheng'] = '摸牌阶段，若你体力值为1或者没有手牌，你可改为获得所有其他角色区域内各一张牌。',
  
  ["tyt_hc__baoling"] = "暴凌",
  [":tyt_hc__baoling"] = "主将技，锁定技，出牌阶段结束时，你移除副将，加3点体力上限并回复3点体力，然后获得〖崩坏〗。",
  ["tyt_hc__benghuai"] = "崩坏",
  [":tyt_hc__benghuai"] = "锁定技，结束阶段，若你不是体力值最小的角色，你选择：1.失去1点体力；2.减1点体力上限；3.执行前两项并获得一个额外的摸牌阶段。",

  ["#tyt_hc__benghuai-both"] = "执行两项并获得摸牌阶段",

  ['$tyt_hc__hengzheng1'] = '老夫进京平乱，岂能空手而归？',
  ['$tyt_hc__hengzheng2'] = '谁的？都是我的！',
  ["$tyt_hc__baoling1"] = "大丈夫，岂能妇人之仁？",
  ["$tyt_hc__baoling2"] = "待吾大开杀戒，哈哈哈哈！",
  ["$tyt_hc__benghuai1"] = "我是不是该减肥了？",
  ["$tyt_hc__benghuai2"] = "呃……",
  ["~tyt_hc_heg__n_dongzhuo"] = "为何人人……皆与我为敌？",
}

local gaoshun = General(extension, "tyt_hc_heg__gaoshun", "qun", 4)

local xunxi = fk.CreateTriggerSkill {
  name = "tyt_hc__xunxi",
  events = { fk.GeneralRevealed },
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(self) and target.phase == Player.NotActive and not target.dead and
    player:canUseTo(Fk:cloneCard("slash"), target, { bypass_distances = true, bypass_times = true })
  end,
  on_cost = function(self, event, target, player, data)
    self.cost_data = { tos = { target.id } }
    return player.room:askForSkillInvoke(player, self.name, data, "#tyt_hc__xunxi-ask::" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local use = { from = player.id, tos = { { target.id } }, card = Fk:cloneCard("slash"), extraUse = true, extra_data = { bypass_distances = true, bypass_times = true } }
    player.room:useCard(use)
  end,
}

local huanjia = fk.CreateTriggerSkill {
  name = "tyt_hc__huanjia",
  frequency = Skill.Compulsory,
  mute = true,
  events = { fk.TargetSpecified, fk.TargetConfirmed },
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self) and data.card.trueName == "slash") and player.room:getCurrent() then return end
    if event == fk.TargetSpecified and (not U.isOnlyTarget(player.room:getPlayerById(data.to), data, event) or player:getMark("huanjia-weapon-turn") > 0) then return end
    if event == fk.TargetConfirmed and player:getMark("huanjia-armor-turn") > 0 then return end
    local equiptype = event == fk.TargetSpecified and Card.SubtypeWeapon or Card.SubtypeArmor
    local t = player.room:getPlayerById(event == fk.TargetSpecified and data.to or data.from)
    self.cost_data = { tos = { t.id }, type = equiptype }
    return t:getEquipment(equiptype) and not player:getEquipment(equiptype)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, event == fk.TargetSpecified and "huanjia-weapon-turn" or "huanjia-armor-turn", 1)
    room:notifySkillInvoked(player, self.name, event == fk.TargetSpecified and "offensive" or "defensive", self.cost_data.tos)
    player:broadcastSkillInvoke(self.name, event == fk.TargetSpecified and 1 or 2)
    local equiptype = self.cost_data.type
    local t = room:getPlayerById(self.cost_data.tos[1])
    local equipid = t:getEquipment(equiptype) --FIXME:国战没神典韦，摆
    if not equipid then return end
    local card = Fk:getCardById(equipid)
    if card.equip_skill and not player:hasSkill(card.equip_skill) then
      local markname = event == fk.TargetSpecified and "tyt_hc__huanjia-weapon-turn" or "tyt_hc__huanjia-armor-turn"
      room:setPlayerMark(player, "@" .. markname, card.name)
      room:setPlayerMark(player, markname, equipid)
      if card.name == "sa__breastplate" then return end --护心镜不允许发动
      room:handleAddLoseSkills(player, card.equip_skill.name, nil, false, true)
    end
  end,

  refresh_events = { fk.AfterCardsMove, fk.AfterTurnEnd },
  can_refresh = function(self, event, target, player, data)
    if player:getMark("@tyt_hc__huanjia-weapon-turn") == 0 and player:getMark("@tyt_hc__huanjia-armor-turn") == 0 then return end
    if event == fk.AfterTurnEnd then return true end
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == Card.PlayerEquip then
        if (player:getMark("@tyt_hc__huanjia-weapon-turn") and player:getEquipment(Card.SubtypeWeapon))
            or (player:getMark("@tyt_hc__huanjia-armor-turn") and player:getEquipment(Card.SubtypeArmor))
        then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterTurnEnd or (player:getMark("@tyt_hc__huanjia-weapon-turn") ~= 0 and player:getEquipment(Card.SubtypeWeapon)) then
      local cid = player:getMark("tyt_hc__huanjia-weapon-turn")
      local card = Fk:getCardById(cid)
      local equip = player:getEquipment(Card.SubtypeWeapon)
      room:setPlayerMark(player, "@tyt_hc__huanjia-weapon-turn", 0)
      room:setPlayerMark(player, "tyt_hc__huanjia-weapon-turn", 0)
      if card and (not equip or equip ~= cid) then --FIXME:国战没相同的装备，摆
        room:handleAddLoseSkills(player, "-" .. card.equip_skill.name, nil, false, true)
      end
    end
    if event == fk.AfterTurnEnd or player:getTableMark("@tyt_hc__huanjia-armor-turn") ~= 0 and player:getEquipment(Card.SubtypeArmor) then
      local cid = player:getMark("tyt_hc__huanjia-armor-turn")
      local card = Fk:getCardById(cid)
      local equip = player:getEquipment(Card.SubtypeArmor)
      room:setPlayerMark(player, "@tyt_hc__huanjia-armor-turn", 0)
      room:setPlayerMark(player, "tyt_hc__huanjia-armor-turn", 0)
      if card and (not equip or equip ~= cid) then
        room:handleAddLoseSkills(player, "-" .. card.equip_skill.name, nil, false, true)
      end
    end
  end,
}

local huanjia_attackrange = fk.CreateAttackRangeSkill {
  name = "#tyt_hc__huanjia_attackrange",
  fixed_func = function(self, from)
    if from:getMark("tyt_hc__huanjia-weapon-turn") ~= 0 then
      local card = Fk:getCardById(from:getMark("tyt_hc__huanjia-weapon-turn"))
      return card.attack_range
    end
  end,
}

huanjia:addRelatedSkill(huanjia_attackrange)
gaoshun:addSkill(xunxi)
gaoshun:addSkill(huanjia)

Fk:loadTranslationTable {
  ["tyt_hc_heg__gaoshun"] = "高顺",
  ["#tyt_hc_heg__gaoshun"] = "清威严整",
  ["illustrator:tyt_hc_heg__gaoshun"] = "聚一_KEVIN",

  ["tyt_hc__xunxi"] = "迅析",
  [":tyt_hc__xunxi"] = "一名角色于其回合外明置武将牌后，你可以视为对其使用【杀】。",
  ["tyt_hc__huanjia"] = "擐甲",
  [":tyt_hc__huanjia"] = "锁定技，每回合各限一次，当你成为【杀】的目标后/使用【杀】指定唯一目标后，若使用者/目标角色的装备区里有防具牌/武器牌且你没有，" ..
      "本回合你视为装备此牌直到你的装备区置入防具牌/武器牌。",

  ["@tyt_hc__huanjia-weapon-turn"] = "擐甲",
  ["@tyt_hc__huanjia-armor-turn"] = "擐甲",
  ["#tyt_hc__xunxi-ask"] = "你想对 %dest 发动〖迅析〗吗？",

  ["$tyt_hc__xunxi1"] = "破阵陷营，虎狼之师。",
  ["$tyt_hc__xunxi2"] = "披坚执锐，所向披靡。",
  ["$tyt_hc__huanjia1"] = "哼，彼之盾挡不住吾之矛。",
  ["$tyt_hc__huanjia2"] = "来吧！怕你不成。",

  ["~tyt_hc_heg__gaoshun"] = "将可断头，岂可折腰。",
}

local duyu = General(extension, "tyt_hc_heg__qunduyu", "qun", 4)

local zhuzhuan = fk.CreateActiveSkill{
  name = "tyt_hc__zhuzhuan",
  anim_type = "special",
  card_num = 1,
  target_num = 0,
  prompt = "#tyt_hc__zhuzhuan-prompt",
  can_use = function(self, player)
    return #player:getTableMark("tyt_hc__zhuzhuan-phase") < 4
  end,
  card_filter = function(self, to_select, selected, player)
    if #selected == 1 then return end
    local c = Fk:getCardById(to_select)
    return not player:prohibitDiscard(c) and c.suit ~= Card.NoSuit and not table.contains(player:getTableMark("tyt_hc__zhuzhuan-phase"), c:getSuitString(true))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local suit = Fk:getCardById(effect.cards[1]):getSuitString(true)
    room:addTableMark(player, "tyt_hc__zhuzhuan-phase", suit)
    room:throwCard(effect.cards, self.name, player)
    room:addTableMark(player, "@tyt_hc__zhuzhuan-phase", suit)
  end,
}

local zhuzhuan_delay = fk.CreateTriggerSkill{
  name = "#tyt_hc__zhuzhuan_delay",
  refresh_events = {fk.PreCardUse},
  mute = true,
  frequency = Skill.Compulsory,
  can_refresh = function(self, event, target, player, data)
    return not player.dead and player == target and #player:getTableMark("@tyt_hc__zhuzhuan-phase") > 0 and (data.card.type == Card.TypeBasic
      or data.card.type == Card.TypeTrick and data.card.sub_type ~= Card.SubtypeDelayedTrick)
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.tyt_hc__zhuzhuan = player:getTableMark("@tyt_hc__zhuzhuan-phase")
    player.room:setPlayerMark(player, "@tyt_hc__zhuzhuan-phase", 0)
  end,
}

local zhuzhuan_delay_spade = fk.CreateTriggerSkill{
  name = "#tyt_hc__zhuzhuan_delay_spade",
  anim_type = "drawcard",
  events = {fk.AfterCardUseDeclared},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and player == target and (data.extra_data or {}).tyt_hc__zhuzhuan and
      table.contains((data.extra_data or {}).tyt_hc__zhuzhuan, "log_spade") and player.hp == 1
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
  end,
}

local zhuzhuan_delay_club = fk.CreateTriggerSkill{
  name = "#tyt_hc__zhuzhuan_delay_club",
  anim_type = "offensive",
  events = {fk.AfterCardUseDeclared, fk.DamageCaused},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      return not player.dead and player == target and (data.extra_data or {}).tyt_hc__zhuzhuan and
      table.contains((data.extra_data or {}).tyt_hc__zhuzhuan, "log_club") and player:isKongcheng()
    else
      return not player.dead and player == target and data.card and data.card:getMark("tyt_hc__zhuzhuan_delay_club") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      player.room:setCardMark(data.card, "tyt_hc__zhuzhuan_delay_club", 1)
    else
      data.damage = data.damage + 1
    end
  end,
}

local zhuzhuan_delay_heart = fk.CreateTriggerSkill{
  name = "#tyt_hc__zhuzhuan_delay_heart",
  anim_type = "special",
  events = {fk.AfterCardUseDeclared, fk.AfterCardTargetDeclared},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      if player.dead or not (player == target and (data.extra_data or {}).tyt_hc__zhuzhuan and
      table.contains((data.extra_data or {}).tyt_hc__zhuzhuan, "log_heart")) then return end
      local players = table.filter(player.room:getOtherPlayers(player, false), function (p)
        return not p:isKongcheng() and not table.contains(TargetGroup:getRealTargets(data.tos), p.id)
      end)
      return #players > 0
    else
      if player.dead or not (player == target and data.card:getMark("tyt_hc__zhuzhuan_delay_heart")> 0)  then return end
      return data.card.trueName ~= "threaten_emperor" and #player.room:getUseExtraTargets(data) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AfterCardUseDeclared then
      local room = player.room
      local players = table.filter(room:getOtherPlayers(player, false), function (p)
        return not p:isKongcheng() and not table.contains(TargetGroup:getRealTargets(data.tos), p.id)
      end)
      if #players == 0 then return end
      local winner, cards = U.askForRaceDiscard(room, players, 1, 1, false, "zhuzhuan_zhuzhan", ".|.|.|.|.|"..data.card:getTypeString(),
      "#zhuzhuan_zhuzhan-invoke:"..player.id.."::"..data.card:getTypeString()..":"..data.card:toLogString())
      if winner then
        self.cost_data = {winner, cards}
        return true
      end
    else
      local room = player.room
      local tos = player.room:askForChoosePlayers(player, room:getUseExtraTargets(data), 1, 1,
            "#variation_addtarget:::"..data.card:toLogString(), self.name, true)
      if #tos > 0 then
        self.cost_data = {tos = tos}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardUseDeclared then
      room:throwCard(self.cost_data[2], self.name, self.cost_data[1], self.cost_data[1])
      room:setCardMark(data.card, "tyt_hc__zhuzhuan_delay_heart", 1)
    else
      table.insert(data.tos, self.cost_data.tos)
    end
  end,
}

local zhuzhuan_delay_diamond = fk.CreateTriggerSkill{
  name = "#tyt_hc__zhuzhuan_delay_diamond",
  anim_type = "control",
  events = {fk.AfterCardUseDeclared},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and player == target and (data.extra_data or {}).tyt_hc__zhuzhuan and
      table.contains((data.extra_data or {}).tyt_hc__zhuzhuan, "log_diamond") and
      table.every(player.room:getOtherPlayers(player, false), function(p)
        return player:getHandcardNum() >= p:getHandcardNum()
      end)
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
  end,
}

zhuzhuan:addRelatedSkill(zhuzhuan_delay)
zhuzhuan:addRelatedSkill(zhuzhuan_delay_spade)
zhuzhuan:addRelatedSkill(zhuzhuan_delay_club)
zhuzhuan:addRelatedSkill(zhuzhuan_delay_heart)
zhuzhuan:addRelatedSkill(zhuzhuan_delay_diamond)
duyu:addSkill(zhuzhuan)

Fk:loadTranslationTable{
  ["tyt_hc_heg__qunduyu"] = "杜预",
  ["#tyt_hc_heg__qunduyu"] = "文成武德",
  ["illustrator:tyt_hc_heg__qunduyu"] = "鬼画府",
  ["tyt_hc__zhuzhuan"] = "注传",
  [":tyt_hc__zhuzhuan"] = "出牌阶段每种花色限一次，你可以弃置一张牌，根据此牌花色令你此阶段使用的下一张基本牌或普通锦囊牌增加一种<a href='tyt_hc_zhuzhuan_href'>"
    .. "强化属性与效果</a>：♠，残躯→摸一张牌；♣，空巢→伤害+1；<font color='red'>♥</font>，助战→目标+1；<font color='red'>♦</font>，富甲→不能被响应。",

  ["tyt_hc_zhuzhuan_href"] = "当你声明使用牌后，若满足强化属性，则获得强化效果。<br>"
    .. "♠，残躯→摸一张牌：若你的体力值为1，你摸一张牌。<br>"
    .. "♣，空巢→伤害+1：若你没有手牌，则此牌造成伤害时，你令伤害+1。<br>"
    .. "<font color='red'>♥</font>，助战→目标+1：除此牌目标外的其他角色同时选择是否弃置一张同类型的手牌，若有角色响应，则此牌选择目标后，你可以额外指定一名角色为目标。<br>"
    .. "<font color='red'>♦</font>，富甲→不能被响应：若你的手牌数为全场最多，所有角色不能响应此牌。",
  ["#tyt_hc__zhuzhuan-prompt"] = "弃置一张牌，根据此牌花色令下一张牌获得效果：♠，残躯→摸一张牌；♣，空巢→伤害+1；♥，助战→目标+1；♦，富甲→不能被响应。",

  ["@tyt_hc__zhuzhuan-phase"] = "注传",
  ["#tyt_hc__zhuzhuan_delay_spade"] = "残躯",
  ["#tyt_hc__zhuzhuan_delay_club"] = "空巢",
  ["#tyt_hc__zhuzhuan_delay_heart"] = "助战",
  ["#tyt_hc__zhuzhuan_delay_diamond"] = "富甲",

  ["#zhuzhuan_zhuzhan-invoke"] = "助战：你可以弃置一张%arg，令%src使用的%arg2可以额外指定一个目标",

  ["$tyt_hc__zhuzhuan1"] = "耽思经籍，博学多通。",
  ["$tyt_hc__zhuzhuan2"] = "广习经籍，只为上能弼国，下可安民。",
  ["~tyt_hc_heg__qunduyu"] = "洛水圆石，遂道向南，吾将以俭自完耳……",
}

local xugong = General(extension, "wl_hc_heg__xugong", "wu", 3)
xugong.subkingdom = "qun"

local biaozhao = fk.CreateActiveSkill{
  name = "wl_hc__biaozhao",
  target_num = 2,
  card_num = 0,
  max_phase_use_time = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return to_select~= player.id and ((#selected == 0 and Fk.currentRoom():getPlayerById(to_select).kingdom ~= "unknown") or
      (#selected == 1 and H.compareKingdomWith(Fk.currentRoom():getPlayerById(to_select), Fk.currentRoom():getPlayerById(selected[1]), true)))
  end,
  on_use = function (self, room, use)
    local player = room:getPlayerById(use.from)
    local targetids = use.tos
    local knowns = table.filter(targetids, function (pid)
      return player:canUseTo(Fk:cloneCard("known_both"), room:getPlayerById(pid))
    end)
    if #knowns == 0 then return end
    if #knowns > 1 then
      knowns = room:askForChoosePlayers(player, knowns, 1, 1, "#wl_hc__biaozhao-known", self.name, false)
    end
    room:useVirtualCard("known_both", nil, player, room:getPlayerById(knowns[1]), self.name)
    table.removeOne(targetids, knowns[1])
    local givetarget = room:getPlayerById(targetids[1])
    if not givetarget.dead and not player:isNude() then
      local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#wl_hc__biaozhao-give::"..givetarget.id)
      room:obtainCard(givetarget, card, false, fk.ReasonGive, player.id, self.name)
      if not player.dead then
        player:drawCards(1, self.name)
      end
    end
  end,
}

local yechou = fk.CreateTriggerSkill{
  name = "wl_hc__yechou",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Death},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true) and data.damage and data.damage.from and not data.damage.from.dead
      and player:canUseTo(Fk:cloneCard("slash"), data.damage.from, {bypass_times = true, bypass_distances = true})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.damage.from
    room:setPlayerMark(player, "wl_hc__yechou-target", to.id)
    if to.dead then return end
    local card1 = Fk:cloneCard("slash")
    card1.skillName = self.name
    local use1 = {
      from = player.id,
      tos = { { to.id } },
      card = card1,
      extraUse = true,
      extra_data = { bypass_distances = true, bypass_times = true, wl_hc__yechoutarget = to.id},
      disresponsiveList = table.map(room.alive_players, Util.IdMapper),
    }
    if player:canUseTo(card1, to, { bypass_distances = true, bypass_times = true}) then
      room:useCard(use1)
    end
    if to.dead then return end
    local card2 = Fk:cloneCard("slash")
    card2.skillName = self.name
    local use2 = {
      from = player.id,
      tos = { { to.id } },
      card = card2,
      extraUse = true,
      extra_data = { bypass_distances = true, bypass_times = true, wl_hc__yechoutarget = to.id},
    }
    for _, p in ipairs(room.alive_players) do
      room:addPlayerMark(p, fk.MarkArmorNullified)
    end
    if player:canUseTo(card2, to, { bypass_distances = true, bypass_times = true}) then
      room:useCard(use2)
    end
    for _, p in ipairs(room.alive_players) do
      room:removePlayerMark(p, fk.MarkArmorNullified)
    end
    if to.dead then return end
    local card3 = Fk:cloneCard("slash")
    card3.skillName = self.name
    local use3 = {
      from = player.id,
      tos = { { to.id } },
      card = card3,
      extraUse = true,
      extra_data = { bypass_distances = true, bypass_times = true, wl_hc__yechoutarget = to.id},
      additionalDamage = 1,
    }
    if player:canUseTo(card3, to, { bypass_distances = true, bypass_times = true}) then
      room:useCard(use3)
    end
  end,

  refresh_events = {fk.EnterDying},
  can_refresh = function(self, event, target, player, data)
    return player.dead and player:hasSkill(self, true, true) and data.damage and data.damage.card and
      table.contains(data.damage.card.skillNames, self.name) and player:getMark("wl_hc__yechou-target") == target.id
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name)
    player:broadcastSkillInvoke(self.name)
    room:addPlayerMark(target, self.name)
    room.logic:getCurrentEvent():findParent(GameEvent.Dying, true):addExitFunc(function ()
      room:removePlayerMark(target, self.name)
    end)
  end,
}
local yechou_prohibit = fk.CreateProhibitSkill{
  name = "#wl_hc__yechou_prohibit",
  prohibit_use = function(self, player, card)
    if card.name == "peach" and not player.dying then
      return table.find(Fk:currentRoom().alive_players, function(p)
        return p.dying and p:getMark(yechou.name) > 0 and H.compareKingdomWith(p, player)
      end)
    end
  end,
}

yechou:addRelatedSkill(yechou_prohibit)
xugong:addSkill(biaozhao)
xugong:addSkill(yechou)

Fk:loadTranslationTable {
  ["wl_hc_heg__xugong"] = "许贡",
  ["#wl_hc_heg__xugong"] = "独计击流",
  ["illustrator:wl_hc_heg__xugong"] = "红字虾",

  ["wl_hc__biaozhao"] = "表召",
  [":wl_hc__biaozhao"] = "出牌阶段限一次，你可选择两名势力不同的其他角色，视为对其中一名角色使用【知己知彼】，然后将一张牌交给另一名角色，若如此做，你摸一张牌。",
  ["wl_hc__yechou"] = "业仇",
  [":wl_hc__yechou"] = "锁定技，你死亡时，视为对杀死你的角色使用三张【杀】，第一张不能被响应，第二张无视防具，第三张伤害值+1，若其以此法进入濒死状态，除其外与其势力相同的角色不能对其使用【桃】。",

  ["#wl_hc__biaozhao-known"] = "表召：选择【知己知彼】的目标",
  ["#wl_hc__biaozhao-give"] = "表召：交给 %dest 一张牌",

  ["$wl_hc__biaozhao1"] = "此人有祸患之像，望丞相慎之。",
  ["$wl_hc__biaozhao2"] = "孙策宜加贵宠，须召还京邑！",
  ["$wl_hc__yechou1"] = "会有人替我报仇的！",
  ["$wl_hc__yechou2"] = "我的门客，是不会放过你的！",
  ["~wl_hc_heg__xugong"] = "终究……还是被其所害……",
}

local simazhao = General(extension, "pt_hc_heg__n_simazhao", "wild", 3)

local suzhi = fk.CreateTriggerSkill {
  name = "pt_hc__suzhi",
  frequency = Skill.Compulsory,
  mute = true,
  events = { fk.CardUsing, fk.DamageCaused, fk.AfterCardsMove, fk.TurnEnd },
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(self) and player.room:getCurrent() then
      if event == fk.CardUsing then
        return target == player and data.card.type == Card.TypeTrick and
        not table.contains(player:getTableMark("@pt_hc__suzhi-turn"), 1)
      elseif event == fk.DamageCaused then
        return target == player and data.card and (data.card.trueName == "slash" or data.card.name == "duel") and
        player.room.logic:damageByCardEffect() and not table.contains(player:getTableMark("@pt_hc__suzhi-turn"), 3)
      elseif event == fk.AfterCardsMove and not table.contains(player:getTableMark("@pt_hc__suzhi-turn"), 2) then
        for _, move in ipairs(data) do
          if move.from and move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            --FIXME:国战暂时没有同时两名角色弃置牌的情况，先鸽
            local from = room:getPlayerById(move.from)
            if from and not (from.dead or from:isNude()) then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                  self.cost_data = move.from
                  return true
                end
              end
            end
          end
        end
      elseif event == fk.TurnEnd then
        return target == player and room.current == player and #player:getTableMark("@pt_hc__suzhi-turn") < 3
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.TurnEnd then
      room:notifySkillInvoked(player, self.name)
      room:setPlayerMark(player, "@@pt_hc__fankui_simazhao", 1)
      room:setPlayerMark(player, "pt_hc__fankui_simazhao-turn", 1) --记录下本回合拿过反馈，回合结束之后不清除
      room:handleAddLoseSkills(player, "pt_hc__fankui", nil)
      return false
    else
      if event == fk.CardUsing then
        room:addTableMark(player, "@pt_hc__suzhi-turn", 1)
        room:notifySkillInvoked(player, self.name, "drawcard")
        player:drawCards(1, self.name)
      elseif event == fk.DamageCaused then
        room:addTableMark(player, "@pt_hc__suzhi-turn", 3)
        room:notifySkillInvoked(player, self.name, "offensive", { data.to.id })
        room:doIndicate(player.id, { data.to.id })
        data.damage = data.damage + 1
      elseif event == fk.AfterCardsMove then
        room:addTableMark(player, "@pt_hc__suzhi-turn", 2)
        room:notifySkillInvoked(player, self.name, "control", { self.cost_data })
        room:doIndicate(player.id, { self.cost_data })
        local card = room:askForCardChosen(player, room:getPlayerById(self.cost_data), "he", self.name)
        room:obtainCard(player.id, card, false, fk.ReasonPrey)
      end
    end
  end,

  refresh_events = { fk.AfterTurnEnd },
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("pt_hc__fankui_simazhao-turn") == 0 and
    player:getMark("@@pt_hc__fankui_simazhao") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player, "-pt_hc__fankui", nil)
    room:setPlayerMark(player, "@@pt_hc__fankui_simazhao", 0)
  end,
}

local zhaoxin = fk.CreateTriggerSkill{
  name = "pt_hc__zhaoxin",
  anim_type = "control",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#pt_hc__zhaoxin-ask")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:showCards(player.player_cards[Player.Hand])
    local targets = table.map(table.filter(room:getOtherPlayers(player, false), function(p)
      return (p:getHandcardNum() <= player:getHandcardNum()) end), Util.IdMapper)
    if #targets > 0 then
      local to = room:getPlayerById(room:askForChoosePlayers(player, targets, 1, 1, "#pt_hc__zhaoxin-choose", self.name, false)[1])
      U.swapHandCards(room, player, player, to, self.name)
    end
  end,
}

local fankui = fk.CreateTriggerSkill{
  name = "pt_hc__fankui",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.from and not data.from.dead then
      if data.from == player then
        return #player.player_cards[Player.Equip] > 0
      else
        return not data.from:isNude()
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from
    room:doIndicate(player.id, {from.id})
    local flag =  from == player and "e" or "he"
    local card = room:askForCardChosen(player, from, flag, self.name)
    room:obtainCard(player.id, card, false, fk.ReasonPrey)
  end
}

simazhao:addSkill(suzhi)
simazhao:addSkill(zhaoxin)
simazhao:addRelatedSkill(fankui)
simazhao:addCompanions("hs__n_simayi")

Fk:loadTranslationTable {
  ["pt_hc_heg__n_simazhao"] = "司马昭",
  ["#pt_hc_heg__n_simazhao"] = "嘲风开天",
  ["illustrator:pt_hc_heg__n_simazhao"] = "凝聚永恒",

  ["pt_hc__suzhi"] = "夙智",
  [":pt_hc__suzhi"] = "锁定技，每回合各限一次：1.当你使用锦囊牌时，你摸一张牌；2.其他角色因弃置而失去牌后，你获得其一张牌；" ..
      "3.你使用【杀】或【决斗】造成伤害时，此伤害+1。回合结束时，若你本回合未执行所有项，你获得“反馈”直至你下个回合结束。",
  ["@pt_hc__suzhi-turn"] = "夙智",
  ["@@pt_hc__fankui_simazhao"] = "夙智 反馈",
  ["pt_hc__zhaoxin"] = "昭心",
  [":pt_hc__zhaoxin"] = "当你受到伤害后，你可展示所有手牌，然后与一名手牌数不大于你的角色交换手牌。",
  ["#pt_hc__zhaoxin-ask"] = "昭心：你可以展示所有手牌，然后与一名手牌数不大于你的角色交换手牌",
  ["#pt_hc__zhaoxin-choose"] = "昭心：选择一名手牌数不大于你的角色，与其交换手牌",
  ["pt_hc__fankui"] = "反馈",
  [":pt_hc__fankui"] = "当你受到伤害后，你可获得来源的一张牌。",

  ["$pt_hc__zhaoxin1"] = "行明动正，何惧他人讥毁。",
  ["$pt_hc__zhaoxin2"] = "大业之举，岂因宵小而动？",
  ["$pt_hc__suzhi1"] = "敌军势大与否，无碍我自计定施。",
  ["$pt_hc__suzhi2"] = "汝竭力强攻，也只是徒燥军心。",
  ["$pt_hc__fankui1"] = "胆敢诽谤惑众，这就是下场！",
  ["$pt_hc__fankui2"] = "今天，就拿你来杀鸡儆猴。",
  ["~pt_hc_heg__n_simazhao"] = "千里之功，只差一步了……",
}

local zhonghui = General(extension, "pt_hc_heg__n_zhonghui", "wild", 4)
zhonghui:addCompanions("ld__n_jiangwei")
local quanji = fk.CreateTriggerSkill {
  name = "pt_hc__quanji",
  mute = true,
  derived_piles = "pt_hc_heg__n_zhonghui_power",
  events = { fk.Damaged, fk.Damage },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not player.dead
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.Damaged then
      room:notifySkillInvoked(player, self.name, "masochism")
    else
      room:notifySkillInvoked(player, self.name, "drawcard")
    end
    player:drawCards(1, self.name)
    if player:isNude() then return end
    if #player:getPile("pt_hc_heg__n_zhonghui_power") < player.maxHp then
      local card = room:askForCard(player, 1, 1, true, self.name, false, nil, "#pt_hc__quanji-push")
      player:addToPile("pt_hc_heg__n_zhonghui_power", card, true, self.name)
    else
      room:askForDiscard(target, 1, 1, true, self.name, false)
    end
  end,
}
local quanji_maxcards = fk.CreateMaxCardsSkill {
  name = "#pt_hc__quanji_maxcards",
  correct_func = function(self, player)
    return player:hasShownSkill(self) and #player:getPile("pt_hc_heg__n_zhonghui_power") or 0
  end,
}
quanji:addRelatedSkill(quanji_maxcards)
local paiyi = fk.CreateActiveSkill {
  name = "pt_hc__paiyi",
  anim_type = "control",
  target_num = 1,
  max_phase_use_time = 1,
  card_filter = function(self, to_select, selected)
    return false
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    --player:broadcastSkillInvoke(self.name)
    if H.askCommandTo(player, target, self.name) then
      if #player:getPile("pt_hc_heg__n_zhonghui_power") == 0 or player.dead then return end
      player:drawCards(#player:getPile("pt_hc_heg__n_zhonghui_power"), self.name)
      if #player:getPile("pt_hc_heg__n_zhonghui_power") > 0 then
        local card = #player:getPile("pt_hc_heg__n_zhonghui_power") == 1 and player:getPile("pt_hc_heg__n_zhonghui_power") or
        room:askForCard(player, 1, 1, false, self.name, false, ".|.|.|pt_hc_heg__n_zhonghui_power", "#pt_hc__quanji-draw",
          "pt_hc_heg__n_zhonghui_power")
        if #card > 0 then
          room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name,
            "pt_hc_heg__n_zhonghui_power", true, player.id)
        end
      end
    else
      if #player:getPile("pt_hc_heg__n_zhonghui_power") == 0 or player.dead then return end
      local targets = table.map(
      table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, target) end), Util.IdMapper)
      if #targets == 0 then return end
      local victim = room:askForChoosePlayers(player, targets, 1, #player:getPile("pt_hc_heg__n_zhonghui_power"),
        "#pt_hc__quanji-damage:::" .. tostring(#player:getPile("pt_hc_heg__n_zhonghui_power")), self.name)
      if #victim > 0 then
        room:sortPlayersByAction(victim)
        for _, id in ipairs(victim) do
          local p = room:getPlayerById(id)
          if not p.dead then
            room:damage {
              from = player,
              to = target,
              damage = 1,
              skillName = self.name,
            }
          end
        end
        if #player:getPile("pt_hc_heg__n_zhonghui_power") > 0 then
          local card = #player:getPile("pt_hc_heg__n_zhonghui_power") <= #victim and
          player:getPile("pt_hc_heg__n_zhonghui_power") or
          room:askForCard(player, #victim, #victim, false, self.name, false, ".|.|.|pt_hc_heg__n_zhonghui_power",
            "#pt_hc__quanji-damageremove:::" .. tostring(#victim), "pt_hc_heg__n_zhonghui_power")
          if #card > 0 then
            room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name,
              "pt_hc_heg__n_zhonghui_power", true, player.id)
          end
        end
      end
    end
  end,
}
zhonghui:addSkill(quanji)
zhonghui:addSkill(paiyi)

Fk:loadTranslationTable {
  ["pt_hc_heg__n_zhonghui"] = "钟会",
  ["#pt_hc_heg__n_zhonghui"] = "桀骜的野心家",
  ["illustrator:pt_hc_heg__n_zhonghui"] = "磐蒲",

  ["pt_hc__quanji"] = "权计",
  [":pt_hc__quanji"] = "当你造成或受到1点伤害后，你可以摸一张牌，然后弃置一张牌，若X小于你的体力上限，你将此牌置于武将牌上，称为“权”；你的手牌上限+X。（X为“权”的数量）",
  ["pt_hc__paiyi"] = "排异",
  [":pt_hc__paiyi"] = "出牌阶段限一次，你可以对一名角色发起军令，若其执行，你摸X张牌，然后移去一张“权”；若其不执行，你可以对与其势力相同的至多X名角色各造成一点伤害，然后移去等量的“权”。（X为“权”的数量）",

  ["#pt_hc__quanji-push"] = "权计：将一张牌置于武将牌上（称为“权”）",
  ["pt_hc_heg__n_zhonghui_power"] = "权",
  ["#pt_hc__quanji-draw"] = "选择一张“权”牌置入弃牌堆",
  ["#pt_hc__quanji-damage"] = "选择至多%arg名角色，对他们各造成一点伤害",
  ["#pt_hc__quanji-damageremove"] = "选择%arg张“权”牌置入弃牌堆",

  ["$pt_hc__quanji1"] = "不露圭角，择时而发！",
  ["$pt_hc__quanji2"] = "晦养厚积，乘势而起！",
  ["$pt_hc__paiyi1"] = "排斥异己，为王者必由之路！",
  ["$pt_hc__paiyi2"] = "非吾友，则必敌也！",
  ["~pt_hc_heg__n_zhonghui"] = "吾机关算尽，却还是棋错一着……",
}

local sunchen = General(extension, "pt_hc_heg__n_sunchen", "wild", 4)

local function getmassacrekingdoms(player)
  local kingdoms = {}
  for _, g in ipairs(player:getTableMark("@&pt_massacre")) do
    local general = Fk.generals[g]
    if general then
      table.insertIfNeed(kingdoms, general.kingdom)
      if general.subkingdom then
        table.insertIfNeed(kingdoms, general.subkingdom)
      end
    end
  end
  return kingdoms
end

local shilus = fk.CreateActiveSkill {
  name = "pt_hc__shilu",
  anim_type = "drawcard",
  target_num = 0,
  max_phase_use_time = 1,
  card_filter = function(self, to_select, selected)
    return false
  end,
  target_filter = function(self, to_select, selected)
    return false
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    --player:broadcastSkillInvoke(self.name)
    player:throwAllCards("h")
    if player.dead or #getmassacrekingdoms(player) == 0 then return end
    room:drawCards(player, math.min(player.maxHp, #getmassacrekingdoms(player)), self.name)
  end,
}
local shilus_trigger = fk.CreateTriggerSkill {
  name = "#shilus_trigger",
  main_skill = shilus,
  anim_type = "special",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    return player == target and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(shilus.name)
    local transformdata = {
      isMain = false,
      isHidden = false,
      num = 3,
    }
    if room.logic:trigger("fk.GeneralTransforming", player, transformdata) then return false end
    local isMain = transformdata.isMain
    local isHidden = transformdata.isHidden
    local num = transformdata.num
    local orig = isMain and player.general or player.deputyGeneral
    num = num or 3
    if not orig then return false end
    if orig == "anjiang" then
      player:revealGeneral(not isMain, true)
      orig = isMain and player.general or player.deputyGeneral
    end
    local existingGenerals = {}
    for _, p in ipairs(room.players) do
      table.insert(existingGenerals, H.getActualGeneral(p, false))
      table.insert(existingGenerals, H.getActualGeneral(p, true))
    end
    local kingdoms, generals = {}, {}
    for i = 1, num, 1 do
      local togeneral = room:findGenerals(function(g)
        local gg = Fk.generals[g]
        if gg.kingdom == "wild" then return false end
        if table.contains(kingdoms, gg.kingdom) then return false end
        if gg.subkingdom ~= nil and table.contains(kingdoms, gg.subkingdom) then return false end
        return true
      end, 1)
      if #togeneral > 0 then
        table.insert(generals, togeneral[1])
        table.insert(kingdoms, Fk.generals[togeneral[1]].kingdom)
        if Fk.generals[togeneral[1]].subkingdom then
          table.insert(kingdoms, Fk.generals[togeneral[1]].subkingdom)
        end
      end
    end
    if #generals == 0 then return end
    local general = room:askForGeneral(player, generals, 1, true) ---@type string
    table.removeOne(generals, general)
    if not orig:startsWith("blank_") then
      table.insert(generals, orig)
    end
    if not isHidden then
      room:changeHero(player, general, false, not isMain, true, false, false)
    else
      if isMain then
        room:setPlayerGeneral(player, "anjiang", true)
        local general = Fk.generals[player:getMark("__heg_general")]
        local skills = table.connect(general.skills, table.map(general.other_skills, Util.Name2SkillMapper))
        for _, s in ipairs(skills) do
          if s.relate_to_place ~= "d" then
            H.addHegSkill(player, s, room)
          end
        end
      else
        room:setDeputyGeneral(player, "anjiang")
        local deputy = Fk.generals[player:getMark("__heg_deputy")]
        if deputy then
          local skills = table.connect(deputy.skills, table.map(deputy.other_skills, Util.Name2SkillMapper))
          for _, s in ipairs(skills) do
            if s.relate_to_place ~= "m" then
              H.addHegSkill(player, s, room)
            end
          end
        end
      end
    end
    room:setPlayerMark(player, isMain and "__heg_general" or "__heg_deputy", general)
    if not isHidden then
      room:changeHero(player, general, false, not isMain, true, false, false)
    else
      -- 暗置变更 --FIXME,等主服修
      local lose = Fk.generals[orig]
      local general = isMain and Fk.generals[player:getMark("__heg_general")] or
      Fk.generals[player:getMark("__heg_deputy")]
      room:setPlayerGeneral(player, "anjiang", isMain)
      local skills = table.connect(general.skills, table.map(general.other_skills, Util.Name2SkillMapper))
      local location = isMain and "d" or "m"
      for _, s in ipairs(skills) do
        if s.relate_to_place ~= location then
          H.addHegSkill(player, s, room)
        end
      end
      for _, s in ipairs(lose:getSkillNameList()) do
        room:handleAddLoseSkills(player, "-" .. s, nil)
      end
    end
    room.logic:trigger("fk.GeneralTransformed", player, orig)
    if not player.dead then
      local massacre = player:getMark("@&pt_massacre")
      if massacre == 0 then massacre = {} end
      table.insertTableIfNeed(massacre, generals)
      room:setPlayerMark(player, "@&pt_massacre", massacre)
    else
      room:returnToGeneralPile(generals)
    end
  end,

  on_lose = function (self, player, is_death)
    local room = player.room
    room:returnToGeneralPile(player:getTableMark("@&pt_massacre"), "bottom")
    room:setPlayerMark(player, "@&pt_massacre", 0)
  end,
}
local xiongnve = fk.CreateTriggerSkill {
  name = "pt_hc__xiongnve",
  frequency = Skill.Compulsory,
  anim_type = "offensive",
  events = { fk.DamageCaused, fk.DamageInflicted },
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not (player == target and player:hasSkill(self) and data.from and not data.from.dead) then return end
    local kingdoms = getmassacrekingdoms(player)
    if event == fk.DamageInflicted then
      self.cost_data = {}
      return table.contains(kingdoms, data.from.kingdom)
    elseif data.card and data.card.trueName == "slash" and player.room.logic:damageByCardEffect() and table.contains(kingdoms, data.to.kingdom) then
      self.cost_data = { tos = { data.to.id } }
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.DamageCaused then
      room:notifySkillInvoked(player, self.name, "offensive", self.cost_data.tos)
      data.damage = data.damage + 1
    elseif data.card and data.card.trueName == "slash" and player.room.logic:damageByCardEffect() then --按因执行其使用的杀的效果的伤害处理吧
      room:notifySkillInvoked(player, self.name, "negative")
      data.damage = data.damage + 1
    else
      room:notifySkillInvoked(player, self.name, "defensive")
      data.damage = data.damage - 1
    end
  end,
}

shilus:addRelatedSkill(shilus_trigger)
sunchen:addSkill(shilus)
sunchen:addSkill(xiongnve)

Fk:loadTranslationTable {
  ["pt_hc_heg__n_sunchen"] = "孙綝",
  ["#pt_hc_heg__n_sunchen"] = "食髓的朝堂客",
  ["illustrator:pt_hc_heg__n_sunchen"] = "depp",

  ["pt_hc__shilu"] = "嗜戮",
  ["#shilus_trigger"] = "嗜戮",
  [":pt_hc__shilu"] = "准备阶段，你可以变更副将（备选武将牌势力各不相同，无次数限制），将剩余的武将牌与被替换的武将牌置于你武将牌上，均称为“戮”；出牌阶段限一次，你可以弃置所有手牌（无牌跳过），然后摸X张牌（X为“戮”的势力数且至多为你的体力上限）。",
  ["pt_hc__xiongnve"] = "凶虐",
  [":pt_hc__xiongnve"] = "锁定技，你受到与“戮”势力相同的角色使用【杀】/非因使用【杀】而造成的伤害时，此伤害+1/-1；当你使用【杀】对与“戮”势力相同的角色造成伤害时，此伤害+1。",

  ["@&pt_massacre"] = "戮",

  ["$pt_hc__shilu1"] = "以杀立威，谁敢反我？",
  ["$pt_hc__shilu2"] = "将这些乱臣贼子，尽皆诛之！",
  ["$pt_hc__xiongnve1"] = "当今天子乃我所立，他敢怎样？",
  ["$pt_hc__xiongnve2"] = "我兄弟三人同掌禁军，有何所惧？",
  ["~pt_hc_heg__n_sunchen"] = "愿陛下念臣昔日之功，陛下？陛下！！",
}

local gongsunyuan = General(extension, "pt_hc_heg__n_gongsunyuan", "wild", 4)
local huaiyi = fk.CreateActiveSkill {
  name = "pt_hc__huaiyi",
  prompt = "#pt_hc__huaiyi-active",
  anim_type = "control",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    --player:broadcastSkillInvoke(self.name)
    local cards = player.player_cards[Player.Hand]
    player:showCards(cards)
    local colors = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(colors, Fk:getCardById(id):getColorString())
    end
    local color = room:askForChoice(player, colors, self.name)
    local throw = {}
    for _, id in ipairs(cards) do
      if Fk:getCardById(id):getColorString() == color then
        table.insert(throw, id)
      end
    end
    room:throwCard(throw, self.name, player, player)
    if player.dead then return end
    local ntargets = table.map(table.filter(room:getOtherPlayers(player, false), function(p) return not p:isNude() end),
      Util.IdMapper)
    if #ntargets == 0 then return end
    local targets = room:askForChoosePlayers(player, ntargets, 1, #throw, "#pt_hc__huaiyi-choose:::" .. tostring(#throw),
      self.name, false)
    if #targets > 0 then
      room:sortPlayersByAction(targets)
      for _, pid in ipairs(targets) do
        if player.dead then return end
        local target = room:getPlayerById(pid)
        if target.dead or target:isNude() then
        else
          local getcards = room:askForCard(target, 1, 998, true, self.name, false, ".",
            "#pt_hc__huaiyi-give::" .. player.id)
          room:setPlayerMark(target, "pt_hc__huaiyi-phase", #getcards)
          room:obtainCard(player, getcards, false, fk.ReasonGive)
        end
      end
      local n, victims = 999, {}
      for _, pid in ipairs(targets) do
        local target = room:getPlayerById(pid)
        if not target.dead then
          if target:getMark("pt_hc__huaiyi-phase") < n then
            if target.kingdom ~= "unknown" and H.hasGeneral(target, true) then
              victims = { pid }
            else
              victims = {}
            end
            n = target:getMark("pt_hc__huaiyi-phase")
          elseif target:getMark("pt_hc__huaiyi-phase") == n then
            if target.kingdom ~= "unknown" and H.hasGeneral(target, true) then
              table.insert(victims, pid)
            end
          end
        end
      end
      table.forEach(targets, function(p) room:setPlayerMark(room:getPlayerById(p), "pt_hc__huaiyi-phase", 0) end)
      if #victims == 0 or player.dead then return end
      local victim = room:askForChoosePlayers(player, victims, 1, 1, "#pt_hc__huaiyi-remove", self.name, false)
      if #victim > 0 then
        H.removeGeneral(room, room:getPlayerById(victim[1]), true)
      end
    end
  end,
}

local zisui = fk.CreateTriggerSkill {
  name = "pt_hc__zisui",
  frequency = Skill.Compulsory,
  mute = true,
  events = { fk.DrawNCards, fk.EventPhaseStart, "fk.GeneralRemoved" },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.DrawNCards and #player:getTableMark("@&pt__infidelity") > 0 and player == target then
      return true
    elseif event == fk.EventPhaseStart and player.phase == Player.Finish and #player:getTableMark("@&pt__infidelity") > player.maxHp and player == target then
      return true
    else
      local e = player.room.logic:getCurrentEvent()
      return e.event == GameEvent.SkillEffect and player == e.data[2] and table.contains(player.room.general_pile, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      room:notifySkillInvoked(player, self.name, "drawcard")
      player:broadcastSkillInvoke(self.name)
      data.n = data.n + #player:getTableMark("@&pt__infidelity")
    elseif event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, self.name, "negative")
      player.room:killPlayer({ who = player.id })
    else
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(self.name)
      room:findGeneral(data)
      room:addTableMark(player, "@&pt__infidelity", data)
    end
  end,

  on_lose = function (self, player, is_death)
    local room = player.room
    room:returnToGeneralPile(player:getTableMark("@&pt__infidelity"), "bottom")
    room:setPlayerMark(player, "@&pt__infidelity", 0)
  end,
}

gongsunyuan:addSkill(huaiyi)
gongsunyuan:addSkill(zisui)

Fk:loadTranslationTable {
  ["pt_hc_heg__n_gongsunyuan"] = "公孙渊",
  ["#pt_hc_heg__n_gongsunyuan"] = "狡黠的投机者",
  ["illustrator:pt_hc_heg__n_gongsunyuan"] = "猎枭",

  ["pt_hc__huaiyi"] = "怀异",
  [":pt_hc__huaiyi"] = "出牌阶段限一次，你可以展示所有手牌并弃置其中一种颜色的所有牌，然后令一至等量名其他角色依次交给你至少一张牌，结算完成后，你令其中一名交给你牌数量最少且明确势力的角色移除副将。",
  ["pt_hc__zisui"] = "恣睢",
  [":pt_hc__zisui"] = "锁定技，被你移除的副将置于此武将牌上，称为“异”；摸牌阶段，你多摸X张牌；结束阶段，若X大于你的体力上限，你死亡。（X为“异”的数量）",

  ["#pt_hc__huaiyi-active"] = "发动 怀异，展示所有手牌，然后选择一种颜色弃置",
  ["#pt_hc__huaiyi-choose"] = "怀异：令至多%arg名角色各交给你牌",
  ["#pt_hc__huaiyi-give"] = "怀异：交给%dest至少一张牌",
  ["#pt_hc__huaiyi-remove"] = "怀异：选择一名角色，移除其副将",
  ["@&pt__infidelity"] = "异",

  ["$pt_hc__huaiyi1"] = "曹魏可王，吾亦可王！",
  ["$pt_hc__huaiyi2"] = "这天下，本就是我囊中之物。",
  ["$pt_hc__zisui1"] = "仲达公，敢问这辽隧之战，谁胜谁负啊，哈哈哈哈……",
  ["$pt_hc__zisui2"] = "凡从我大燕者，授印封爵，全族俱荣！",
  ["~pt_hc_heg__n_gongsunyuan"] = "流星骤损，三军皆溃，看来大势去矣……",
}

return extension
